window.onload = Buttons;

function Buttons()
{
	 setCTime();
	 changeColor();

	 var b1 = document.getElementById("button1");
	 b1.onclick = calc;

	 var b2 = document.getElementById("imageButton");
	 b2.onclick = changeImage;

	 var b3 = document.getElementById("ctCreate");
	 b3.onclick = createColorTable;

	 var b4 = document.getElementById("ctRemove");
	 b4.onclick = removeColorTable;

	 var b5 = document.getElementById("button5");
	 b5.onclick = stopTextColor;

	 var b6 = document.getElementById("button6");
	 b6.onclick = myMove;

	 var b7 = document.getElementById("button7");
	 b7.onclick = guess;

	 var b8 = document.getElementById("button8");
	 b8.onclick = replay;

	 var b9 = document.getElementById("guessbutton");
	 b9.onclick = guessLetter;

	 var b10 = document.getElementById("button9");
	 b10.onclick = newGame;

}

function calc()
{
	var x = document.getElementById("x").value;
	var y = document.getElementById("y").value;
	var sum;
	sum = parseInt(x) + parseInt(y);
	document.getElementById("sum").value = sum;
}

var nGuesses = 0;
var computerNumber = Math.floor(Math.random() * 100 + 1);
function guess() 
{	
	var result ="";
	nGuesses++;
	var input = parseInt(document.getElementById("user").value);
	if(input == computerNumber)
		result = "정답입니다";
	else if(input > computerNumber)
		result = "큽니다";
	else
		result = "작습니다";
	document.getElementById("guesses").value = nGuesses;
	document.getElementById("result").value = result;
	document.getElementById("check").value = computerNumber;
}

function replay()
{
	guessTime = 0;
	document.getElementById("user").value = "";
	document.getElementById("guesses").value = "";
	document.getElementById("result").value = "";
	document.getElementById("check").value = "";
	computerNumber = Math.floor(Math.random() * 100 + 1);
}

var MONTH = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function setCTime()
{
	var now = new Date();
	var month = MONTH[now.getMonth()];
	var s = month + " " + now.getDate() + ". " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
	document.getElementById("ctime").innerHTML = s;
	setTimeout(setCTime, 1000);
}

//constants
var POSSIBLE_WORDS = ["obdurate", "verisimilitude", "defenestrate", "obsequious", "dissonant"
	, "today", "idempotent"];
var MAX_GUESSES = 6;

//global variables
var guesses = " ";
var guessCount = MAX_GUESSES;
var word;

function newGame(){
	var randomIndex = parseInt(Math.random() * POSSIBLE_WORDS.length);
	word = POSSIBLE_WORDS[randomIndex];

	guessCount = MAX_GUESSES;
	guesses = "";
	var guessButton = document.getElementById("guessbutton");
	guessButton.disabled = false;
	updatePage();  //show initial word clue - all underscores
}

function updatePage(){
	var clueString = "";
	for(var i = 0; i < word.length; i++)
	{
		var letter = word.charAt(i);
		if(guesses.indexOf(letter) >= 0) //letter has been guessed
		{
			clueString += letter + " ";
		} else {  //not guessed
			clueString += "_ ";
		}
	}

	var clue = document.getElementById("clue");
	clue.innerHTML = clueString;

	var guessArea = document.getElementById("guessstr");
	if(guessCount == 0)
	{
		guessArea.innerHTML = "You lose."; //game over(lose)
	} else if (clueString.indexOf("_") < 0) {
		guessArea.innerHTML = "You win!!!"; //game over(win)
	} else {
		guessArea.innerHTML = "Guesses:" + guesses;
	}
	var	image = document.getElementById("hangmanpic");
	image.src = "hangman" + guessCount + ".gif";
}

function guessLetter()
{
	var input = document.getElementById("hguess");
	var clue = document.getElementById("clue");//+++
	var letter = input.value;
	if(guesses.indexOf(letter) >= 0)	//用于检查玩家输入的字母是否已被猜过
	{
		return;
	}
	guesses += letter;
	if(word.indexOf(letter) < 0)
	{
		guessCount--;  //an incorrect guess
	}
	updatePage();
}


function changeImage()
{
	var bimg = document.getElementById("image");
	var sarray = bimg.src.split('/');
	var str = sarray[sarray.length - 1];
	if(str == "1.jfif")
		bimg.src = "2.jfif";
	else
		bimg.src = "1.jfif";
}

var colorNames = ["maroon", "red", "orange", "yellow", "olive", "purple", "fuchsia", "white","lime",
	"green", "navy", "blue", "aqua", "teal", "black", "silver", "gray"];

function createColorTable()
{
	var colordiv = document.getElementById("colorTable");

	for(var i = 0; i < colorNames.length; i++)
	{
		var ndiv = document.createElement("div");
		ndiv.setAttribute("class", "ctbox");
		ndiv.innerHTML = colorNames[i];
		ndiv.style.display = "inline-block";
		ndiv.style.width = "60px";
		ndiv.style.padding = "10px";
		ndiv.style.backgroundColor = colorNames[i];
		colordiv.appendChild(ndiv);
	}
}


function removeColorTable()
{
	var parent = document.getElementById("colorTable");
	var child = parent.childNodes;

	while(child[0])
		parent.removeChild(child[0]);
}


var id;
function changeColor()
{
 	id = setInterval(flashText, 1000);
}

function flashText()
{
	var elem = document.getElementById("target");
	elem.style.color = (elem.style.color == "red")?"blue" : "red";
	elem.style.backgroundColor = (elem.style.backgroundColor == "green")?"yellow" : "green";
}

function stopTextColor()
{
	clearInterval(id);
}


function myMove()
{
	var elem = document.getElementById("animate");
	var pos = 0;
	var id = setInterval(frame, 5);

 function frame()
 {		
 	if(pos == 350)
 	{
 		clearInterval(id);
 	} else {
 	pos++;
 	elem.style.top = pos + 'px';
 	elem.style.left = pos + 'px';
  }
 }
}


